package com.ocom.payhandle.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.*;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.GetRecentOrderRecordsRequest;
import com.ocom.common.request.payhandle.QrCodeBillListRequest;
import com.ocom.common.request.payhandle.QrCodeMachDailySummaryRequest;
import com.ocom.common.request.ugcapi.order.RecentAddressRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.ABCChargeConfirmVo;
import com.ocom.common.vo.pay.CallBackVo;
import com.ocom.common.vo.payconfig.kitchenprinter.PlanKitchenPrinterRequest;
import com.ocom.common.vo.pyhandle.LimitProcessVo;
import com.ocom.common.vo.pyhandle.ShoppingCartRecordVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfRecordProcess;
import com.ocom.payhandle.feign.PayConfigClientService;
import com.ocom.payhandle.mq.business.saveorder.SaveOrderProductService;
import com.ocom.payhandle.service.*;
import com.ocom.payhandle.vo.MqSaveOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class XfRecordProcessServiceImpl extends ServiceImpl<IDaoXfRecordProcess, EntityXfRecordProcess> implements XfRecordProcessService {

    @Autowired
    private IDaoXfRecordProcess iDaoXfRecordProcess;

    @Autowired
    private XfAccountInfoService xfAccountInfoService;

    @Autowired
    private VoucherDataService voucherDataService;

    @Autowired
    private XfSubAccountListService xfSubAccountListService;

    @Autowired
    private SaveOrderProductService saveOrderProductService;

    @Resource
    private FeeDataService feeDataService;

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private XfGoodsListProcessService xfGoodsListProcessService ;

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public int insertEntityXfRecord(Long companyID, EntityXfRecordProcess entityXfRecord) {
        return iDaoXfRecordProcess.insert(entityXfRecord);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public int updateEntityXfRecord(Long companyID, EntityXfRecordProcess updateXfRecordProcess) {
        return iDaoXfRecordProcess.updateById(updateXfRecordProcess);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public EntityXfRecordProcess selectTradeNo(Long companyID, String tradeNo, String machNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.in("order_no", tradeNo, "DK" + tradeNo, "LX" + tradeNo);
        if (!CommonUtil.isNull(machNo)) {
            queryWrapper.eq("device_no", machNo);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return iDaoXfRecordProcess.selectOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityXfRecordProcess selectTradeNo(Long companyId, Long id) {
        if (id == null) {
            return null;
        }
        return iDaoXfRecordProcess.selectById(id);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public PageInfo<EntityXfRecordProcess> selectPageInfo(Long companyID, QrCodeBillListRequest request,Long pId) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyID);
        queryWrapper.eq("device_no", request.getMachNo());
        queryWrapper.eq("cons_state", request.getConsState());
        if (pId != null && pId != 0l) {
            queryWrapper.eq("p_id", pId );
        }
        queryWrapper.eq("pay_state", 1);
        queryWrapper.orderByDesc("cons_time");
        PageHelper.startPage(request.getPageIndex(), request.getSize());
        List<EntityXfRecordProcess> pointInfoPageInfo = this.list(queryWrapper);
        PageInfo<EntityXfRecordProcess> pageInfo = new PageInfo<>(pointInfoPageInfo);
        return pageInfo;
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public List<EntityXfRecordProcess> selectMachDailySummary(Long companyID, QrCodeMachDailySummaryRequest request) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.select("sum(real_money) as amount", "count(id) as times", "op_type_diff", "op_type");
        queryWrapper.eq("com_id", companyID);
        queryWrapper.eq("device_no", request.getMachNo());
        queryWrapper.eq("pay_state", 1);
        queryWrapper.in("cons_state", 1, 2, 3, 4, 5);
//        queryWrapper.apply("date_format(create_time, '%Y-%m-%d')={0}", request.getQueryDate());
        queryWrapper.between("create_time", request.getQueryDate()+" 00:00:00",request.getQueryDate()+" 23:59:59");
        queryWrapper.groupBy("op_type_diff");
        return iDaoXfRecordProcess.selectList(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyID")
    @Override
    public List<EntityXfRecordProcess> selectMachDailySummary2(Long companyID, QrCodeMachDailySummaryRequest request) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.select("sum(real_money) as amount", "count(id) as times", "meal_no");
        queryWrapper.eq("com_id", companyID);
        queryWrapper.eq("device_no", request.getMachNo());
        queryWrapper.eq("pay_state", 1);
        queryWrapper.in("cons_state", 1, 2, 3, 5);
//        queryWrapper.apply("date_format(create_time, '%Y-%m-%d')={0}", request.getQueryDate());
        queryWrapper.between("create_time", request.getQueryDate()+" 00:00:00",request.getQueryDate()+" 23:59:59");
        queryWrapper.groupBy("meal_no");
        return iDaoXfRecordProcess.selectList(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId")
    public EntityXfRecordProcess getRecordByOrderno(Long companyId, Long pId, String orderNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        if (null != pId) {
            queryWrapper.eq("p_id", pId);
        }
        queryWrapper.eq("order_no", orderNo);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId")
    public boolean sendByOrderno(Long companyId, String orderNo) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("order_no", orderNo);

        updateWrapper.set("send_state", 1);
        return update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId")
    public List<EntityXfRecordProcess> getNotsendRecord(Long companyId, String prefix, List<Integer> consStates, List<Integer> payState, int size) {
        if (companyId == null) {
            return new ArrayList<>();
        }
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        if (null != companyId) {
            queryWrapper.eq("com_id", companyId);
        }
        if (StringUtils.isNotBlank(prefix)) {
            queryWrapper.likeRight("order_no", prefix);
        }
        if (null != consStates) {
            queryWrapper.in("cons_state", consStates);
        }

        if (null != payState) {
            queryWrapper.in("pay_state", payState);
        }
        queryWrapper.eq("send_state", 0);

//        PageHelper.startPage(0, size <= 0 ? 500 : size);
        queryWrapper.orderByAsc("id");
        queryWrapper.last("limit 500");
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Result callback(Long companyId, CallBackVo callBackVo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_state", 0);
        queryWrapper.eq("com_id", callBackVo.getCompanyId());
        queryWrapper.eq("plat_order_no", callBackVo.getPlatOrderNo());
        queryWrapper.last("limit 1 for update");
        EntityXfRecordProcess entityXfRecordProcess = this.getOne(queryWrapper);
        if (entityXfRecordProcess == null) {
            return ResultUtil.error(ResultEnum.Err_4025);
        }

        //1. 修改订单状态
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("pay_state", callBackVo.getStatus());
        updateWrapper.eq("pay_state", 0);
        updateWrapper.eq("com_id", callBackVo.getCompanyId());
        updateWrapper.eq("plat_order_no", callBackVo.getPlatOrderNo());

        try {
            String dateStr = callBackVo.getPayTime();
            Date payDate = DateUtils.strToDate(dateStr.substring(0, 4) + "-" +
                    dateStr.substring(4, 6) + "-" +
                    dateStr.substring(6, 8) + " " +
                    dateStr.substring(8, 10) + ":" +
                    dateStr.substring(10, 12) + ":" +
                    dateStr.substring(12, 14)
            );
            updateWrapper.set("pay_time", payDate);
        } catch (Exception e) {

        }

        boolean b = this.update(updateWrapper);
        if (!b) {
            return ResultUtil.error(ResultEnum.Err_4026);
        }

        //成功    100 话费 ？ 101 缴费？
        if (callBackVo.getStatus() == 1 && entityXfRecordProcess.getPId() > 0l && entityXfRecordProcess.getConsType() != 100 && entityXfRecordProcess.getConsState() != 111
                && entityXfRecordProcess.getConsState() != 3  && entityXfRecordProcess.getConsState() != 5 ) {
            if(entityXfRecordProcess.getSubAccount() == null || entityXfRecordProcess.getSubAccount()==0){
                //2. 加金额
                EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.chageUserMoney(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                return ResultUtil.success(entityXfAccountInfo);
            }else{
                xfSubAccountListService.chageUserMoney(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), entityXfRecordProcess.getSubAccount(), entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                return ResultUtil.success();
            }
        }

        //失败
        if (callBackVo.getStatus() == 2 && entityXfRecordProcess.getConsState() != 111) {
            return ResultUtil.success();
        }

        if (entityXfRecordProcess.getConsState() == 111) {
            if (callBackVo.getStatus() == 1) {
                return ResultUtil.success(feeDataService.updateFeeStatus(entityXfRecordProcess.getComId(), entityXfRecordProcess.getReserved2(), entityXfRecordProcess.getOrderNo(),Math.abs(entityXfRecordProcess.getRealMoney())));
            } else {
                return ResultUtil.success();
            }
        }

        return ResultUtil.success();
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    @Override
    public PageInfo<EntityXfRecordProcess> selectPayment(Long comId, Integer consState,Long minId,Integer page ,Integer size) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_state", 0);
        queryWrapper.eq("cons_state", consState);
        queryWrapper.gt("op_type_diff", 200);
        if(minId>0){
            queryWrapper.lt("id", minId);
        }
        long time = 30*60*1000;//30分钟
        Date beforeDate = new Date( new Date().getTime() - time);//30分钟前的时间
        if(consState == 10){
            queryWrapper.between("cons_time", DateUtil.addDays(new Date(),-10),beforeDate);
        }else{
            queryWrapper.between("cons_time", DateUtil.addDays(new Date(),-5),beforeDate);
        }
//        queryWrapper.apply("DATE_SUB(CURDATE(), INTERVAL 3 DAY) <=date(cons_time)");
//        queryWrapper.apply("cons_time<CURRENT_TIMESTAMP - INTERVAL 30 MINUTE");
        queryWrapper.orderByDesc("id");
//        queryWrapper.last("limit 500");
//        return iDaoXfRecordProcess.selectList(queryWrapper);
        PageHelper.startPage(page, size);
        List<EntityXfRecordProcess> xfRecordProcessList = this.list(queryWrapper);
        PageInfo<EntityXfRecordProcess> pageInfo = new PageInfo<>(xfRecordProcessList);
        return pageInfo;
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    @Override
    public List<EntityXfRecordProcess> selectPayment(Long comId, Integer consState, Long opTypeDiff, String deviceNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_state", 0);
        queryWrapper.eq("cons_state", consState);
        queryWrapper.eq("op_type_diff", opTypeDiff);
        queryWrapper.eq("device_no", deviceNo);
        queryWrapper.apply("DATE_SUB(CURDATE(), INTERVAL 3 DAY) <=date(cons_time)");
        queryWrapper.apply("cons_time<CURRENT_TIMESTAMP - INTERVAL 30 MINUTE");
        queryWrapper.orderByAsc("id");
        return iDaoXfRecordProcess.selectList(queryWrapper);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public Result<EntityXfRecordProcess> abcChargeConfirm(Long comId, ABCChargeConfirmVo vo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", vo.getOrderNo());
        queryWrapper.last("limit 1 for update");
        EntityXfRecordProcess entityXfRecordProcess = this.getOne(queryWrapper);

        if (null == entityXfRecordProcess) {
            return ResultUtil.error(ResultEnum.Err_4101);
        } else if (!entityXfRecordProcess.getPayState().equals(0) || !vo.getComId().equals(entityXfRecordProcess.getComId()) || !Long.valueOf(vo.getPId()).equals(entityXfRecordProcess.getPId())) {
            return ResultUtil.error(ResultEnum.Err_4100);
        }

        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", vo.getOrderNo());
        updateWrapper.set("charge_balance", vo.getAccountRrechargeMoney() + vo.getAmount());
        updateWrapper.set("sub_balance", vo.getAccountSubsidyMoney());
        updateWrapper.set("cons_time", new Date());
        updateWrapper.set("meal_date", DateUtils.dateTime(new Date()));
        updateWrapper.set("cons_money", vo.getAmount());
        updateWrapper.set("pay_time", vo.getPayTime());
        updateWrapper.set("pay_state", 1);
        updateWrapper.set("charge_money", vo.getAmount());
        updateWrapper.set("real_money", vo.getAmount());

        boolean flag = this.update(updateWrapper);

        if (flag) {
            //加余额
            xfAccountInfoService.chageUserMoney(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), vo.getAmount().intValue(), 0);
            //同步到成功表
//            try {
//                MqSaveOrderVo mqSaveOrderVo = new MqSaveOrderVo();
//                mqSaveOrderVo.setComId(entityXfRecordProcess.getComId());
//                mqSaveOrderVo.setId(entityXfRecordProcess.getId());
//                mqSaveOrderVo.setMachNo(entityXfRecordProcess.getDeviceNo());
//                saveOrderProductService.saveOrder(JSON.toJSONString(mqSaveOrderVo));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }

        }

        return flag ? ResultUtil.success(entityXfRecordProcess) : ResultUtil.error(1);
    }


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public Result abcChargeFail(Long comId, String orderNo) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderNo);
        updateWrapper.set("pay_state", 2);

        boolean flag = this.update(updateWrapper);

        return flag ? ResultUtil.success() : ResultUtil.error(1);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public List<EntityXfRecordProcess> getRecentOrderRecords(Long comId, GetRecentOrderRecordsRequest request) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("cons_state", 3);
        if (StringUtils.isNotBlank(request.getPayStates())) {
            queryWrapper.in("pay_state", request.getPayStates().split(","));
        }
        if (StringUtils.isNotBlank(request.getOpenId())) {
            queryWrapper.eq("card_no", request.getOpenId());
        }
        if (StringUtils.isNotBlank(request.getBeginDate())) {
            queryWrapper.gt("cons_time", request.getBeginDate());
        }
        if (null != request.getPointId() && request.getPointId() > 0) {
            queryWrapper.eq("point_id", request.getPointId());
        }
        if (null != request.getPId() && request.getPId() > 0) {
            queryWrapper.eq("p_id", request.getPId());
        }
        queryWrapper.orderByDesc("cons_time");
        PageHelper.startPage(request.getPage(), request.getSize());

        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public PageInfo<EntityXfRecordProcess> getRecentAddress(Long comId, RecentAddressRequest request) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("receipt_id", -1);
        queryWrapper.eq("cons_state", 3);
        queryWrapper.eq("card_no", request.getOpenId());
        queryWrapper.orderByDesc("cons_time");
        PageHelper.startPage(request.getPage(), request.getSize());
        List<EntityXfRecordProcess> pointInfoPageInfo = this.list(queryWrapper);
        PageInfo<EntityXfRecordProcess> pageInfo = new PageInfo<>(pointInfoPageInfo);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean verificationEntityXfRecord(Long companyId, String orderNo) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderNo);
        updateWrapper.eq("com_id", companyId);
        updateWrapper.set("verification", 1);
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateOrgEntityXfRecord(Long companyId, Long id) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("if_refund", 0);
        updateWrapper.set("refund_time", new Date());
        updateWrapper.setSql("if_refund = if_refund+1");
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateOrgEntityXfRecordB(Long companyId, Long id) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("com_id", companyId);
        updateWrapper.set("refund_time", new Date());
        updateWrapper.setSql("if_refund = if_refund+1");
        return this.update(updateWrapper);
    }


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean saveLxOrder(Long companyId,EntityXfRecordProcess entityXfRecordProcess, Integer subAccount, Integer accountId) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", entityXfRecordProcess.getId());
        updateWrapper.set("pay_time", new Date());
        updateWrapper.set("pay_state", 1);
        boolean up = this.update(updateWrapper);
        if (!up) {
            return false;
        }
        //使用优惠券
        if (entityXfRecordProcess.getVoucherNumber() != null &&
                entityXfRecordProcess.getVoucherNumber() > 0) {
            if (!voucherDataService.useVoucherNo(entityXfRecordProcess.getComId(), entityXfRecordProcess.getVoucherNo(), entityXfRecordProcess.getVoucherNumber())) {
                throw new RuntimeException("使用优惠券失败");
            }
        }
        //使用账户
        if(!(entityXfRecordProcess.getChargeMoney()==0l && entityXfRecordProcess.getSubMoney() ==0l)){
            if (subAccount == 0) {
                xfAccountInfoService.chageUserMoney(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
            } else {
                xfSubAccountListService.chageUserMoney(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), subAccount, entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
            }
        }

        return true;
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean saveLxOrder2(Long companyId,EntityXfRecordProcess entityXfRecordProcess, Integer subAccount, Integer accountId) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", entityXfRecordProcess.getId());
        updateWrapper.set("pay_time", new Date());
        updateWrapper.set("pay_state", 1);


        //使用账户
        if(!(entityXfRecordProcess.getChargeMoney()==0l && entityXfRecordProcess.getSubMoney() ==0l)){
            if (subAccount == 0) {
                EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.changeUserMoney2(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                updateWrapper.set("charge_balance", entityXfAccountInfo.getAccountRechargeMoney());
                updateWrapper.set("sub_balance", entityXfAccountInfo.getAccountSubsidyMoney());

            } else {
                EntityXfSubAccountList entityXfSubAccountList = xfSubAccountListService.chageUserMoney2(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), subAccount, entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                updateWrapper.set("charge_balance", entityXfSubAccountList.getAccountRechargeMoney());
                updateWrapper.set("sub_balance", entityXfSubAccountList.getAccountSubsidyMoney());
            }
        }
        boolean up = this.update(updateWrapper);
        if (!up) {
            return false;
        }
        return true;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Result getWaitPayLimit(Long companyId, EntityXfRecord entityXfRecord) {
        Integer mealNo = Integer.valueOf(entityXfRecord.getMealNo());
        String beginDate =DateUtils.getDate();
        String endDate=DateUtils.getDate();
        if(mealNo==500 || mealNo==1000){
            if(mealNo==1000){
                SimpleDateFormat sdf = new SimpleDateFormat("MM");
                String format = sdf.format(new Date());
                int month = Integer.parseInt(format);

                beginDate = DateUtils.getFirstDayOfMonth(month);
                endDate = DateUtils.getLastDayOfMonth(month);
            }
            mealNo=-1;
        }
        LimitProcessVo limitProcessVo = iDaoXfRecordProcess.getWaitPayLimit(companyId,entityXfRecord.getPId(),mealNo,beginDate,endDate);

        return ResultUtil.success(limitProcessVo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> getWaitPayList(Long companyId, String deviceNo, String origOrderNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("pay_state", 1);
        queryWrapper.eq("if_refund", 0);
        queryWrapper.eq("cons_state", 1);
        queryWrapper.eq("device_no", deviceNo);
        queryWrapper.eq("orig_order_no", origOrderNo);
        queryWrapper.orderByDesc("cons_time");
        return this.list(queryWrapper);


    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateDkRecord(Long companyId, String origOrderNo) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("order_no", "DK" + origOrderNo);
        updateWrapper.set("plat_order_no", "LX" + origOrderNo);
        updateWrapper.set("orig_order_no", origOrderNo);
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> selectByOrderNos(Long companyId, List<String> orderNos) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.in("order_no", orderNos);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateXfFace(Long companyId, EntityXfRecord entityXfRecord) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("device_no", entityXfRecord.getDeviceNo());
        updateWrapper.eq("month_value", entityXfRecord.getMonthValue());
        updateWrapper.in("order_no", entityXfRecord.getOrderNo(), "DK" + entityXfRecord.getOrderNo(), "LX" + entityXfRecord.getOrderNo());
        updateWrapper.set("face", entityXfRecord.getFace());
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> getMqFailList(Long companyId,List<Integer> ids) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.select("com_id", "device_no","id");
        queryWrapper.in("id", ids);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> getRefundOrders(Long companyId) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.select("com_id", "device_no","id","order_no","orig_order_no","pay_channel_no");
        queryWrapper.eq("cons_state", 2);
        queryWrapper.eq("pay_state", 0);
        queryWrapper.gt("op_type_diff", 200);
        long time = 30*60*1000;//30分钟
        Date beforeDate = new Date( new Date().getTime() - time);//30分钟前的时间
        queryWrapper.between("cons_time", DateUtil.addDays(new Date(),-3),beforeDate);
        queryWrapper.orderByAsc("cons_time");
        queryWrapper.last("limit 500");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void refundHandle(Long companyId,String deviceNo,String origOrderNO ,String refundOrderNO,Date refundDate) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("device_no", deviceNo);
        updateWrapper.eq("order_no", origOrderNO);
        updateWrapper.eq("if_refund", 0);
        updateWrapper.eq("pay_state", 1);
        updateWrapper.set("refund_time",refundDate);
        updateWrapper.setSql("if_refund = if_refund+1");
        boolean bp =  this.update(updateWrapper);
        if (!bp){
            throw new RuntimeException("修改原记录失败");
        }

        UpdateWrapper<EntityXfRecordProcess> updateWrapper1 = Wrappers.update();
        updateWrapper1.eq("com_id", companyId);
        updateWrapper1.eq("device_no", deviceNo);
        updateWrapper1.eq("order_no", refundOrderNO);
        updateWrapper1.eq("pay_state",0);
        updateWrapper1.set("pay_state",1);
        updateWrapper1.set("pay_time",refundDate);
        boolean bp1 = this.update(updateWrapper1);
        if (!bp1){
            throw new RuntimeException("修改退款订单失败");
        }

//        UpdateWrapper<EntityXfRecordProcess> updateWrapper2 = Wrappers.update();
//        updateWrapper2.eq("com_id", companyId);
//        updateWrapper2.eq("device_no", deviceNo);
//        updateWrapper2.eq("cons_state", 2);
//        updateWrapper2.eq("orig_order_no", origOrderNO);
//        updateWrapper2.ne("order_no", refundOrderNO);
//        updateWrapper2.set("pay_state",2);
//        this.update(updateWrapper2);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> getDelayPayList(Long companyId, Integer payState, Integer consState) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
//        queryWrapper.select("com_id", "device_no","id","order_no","orig_order_no","pay_channel_no");
        queryWrapper.eq("cons_state", consState);
        queryWrapper.eq("pay_state", payState);
        queryWrapper.eq("meal_date", DateUtil.getDate());
        queryWrapper.gt("create_time", DateUtil.getDateBefore(new Date(),3));
        queryWrapper.orderByAsc("id");
        queryWrapper.last("limit 50");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<EntityXfRecordProcess> cacelDealyPayList(Long companyId) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("cons_state", 205);
        queryWrapper.eq("pay_state", 0);
        queryWrapper.eq("op_type_diff", 306l);
        queryWrapper.eq("meal_date", DateUtil.getDate());
        queryWrapper.gt("create_time", DateUtil.getDateBefore(new Date(),3));
        queryWrapper.last("limit 100");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean cacelDealyPay(Long companyId,Long id) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = Wrappers.update();
        updateWrapper.eq("id", id);
        updateWrapper.eq("cons_state", 205);
        updateWrapper.eq("pay_state", 0);
        updateWrapper.eq("op_type_diff", 306l);
        updateWrapper.eq("meal_date", DateUtil.getDate());
        updateWrapper.set("pay_state", 4);
        updateWrapper.set("if_refund", 1);
        updateWrapper.set("reserved2", "定时任务失败订单");
        return this.update(updateWrapper);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean saveShoppingCard(Long companyId, List<ShoppingCartRecordVo> recordVoList) {
        try {
            List<EntityXfRecordProcess> entityXfRecordProcessList = new ArrayList<>(recordVoList);
            boolean b1 = this.saveBatch(entityXfRecordProcessList);
            if(!b1)   return  false;
            List<EntityXfGoodsListProcess> goodsListProcesses = new ArrayList<>();
            for (ShoppingCartRecordVo shoppingCartRecordVo:recordVoList){
                goodsListProcesses.addAll(shoppingCartRecordVo.getGoodList());
            }
            boolean b2 = xfGoodsListProcessService.addXfGoodsListProcessList(companyId,goodsListProcesses);
            if(!b2)   return  false;
            return  true;
        }catch (Exception exception){
            log.info("插入订单失败 【{}】【{}】",recordVoList.get(0).getOrderNo(),exception);
            return  false;
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateShoppingCard(Long companyId, List<ShoppingCartRecordVo> recordVoList) {
        return false;
    }


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean saveLxOrder3(Long companyId,EntityXfRecordProcess entityXfRecordProcess, Integer subAccount, Integer accountId) {
        UpdateWrapper<EntityXfRecordProcess> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("plat_order_no", entityXfRecordProcess.getPlatOrderNo());
        updateWrapper.eq("com_id", companyId);
        updateWrapper.set("pay_time", new Date());
        updateWrapper.set("pay_state", 1);
        updateWrapper.set("op_type_diff", 100);
        updateWrapper.setSql("charge_money = real_money " );

        //使用账户
        if(!(entityXfRecordProcess.getChargeMoney()==0l && entityXfRecordProcess.getSubMoney() ==0l)){
            if (subAccount == 0) {
                EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.changeUserMoney2(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                updateWrapper.set("charge_balance", entityXfAccountInfo.getAccountRechargeMoney());
                updateWrapper.set("sub_balance", entityXfAccountInfo.getAccountSubsidyMoney());
            } else {
                EntityXfSubAccountList entityXfSubAccountList = xfSubAccountListService.chageUserMoney2(entityXfRecordProcess.getComId(), entityXfRecordProcess.getPId(), subAccount, entityXfRecordProcess.getChargeMoney().intValue(), entityXfRecordProcess.getSubMoney().intValue());
                updateWrapper.set("charge_balance", entityXfSubAccountList.getAccountRechargeMoney());
                updateWrapper.set("sub_balance", entityXfSubAccountList.getAccountSubsidyMoney());
            }
        }
        boolean up = this.update(updateWrapper);
        if (!up) {
            return false;
        }
        return true;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordProcess> cartOrders(Long companyId, String platOrderNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.select("order_no","id","device_no");
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("cons_state", 5);
        queryWrapper.eq("pay_state", 1);
        queryWrapper.eq("plat_order_no", platOrderNo);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Result xfUserparam(Long companyId,EntityXfRecord entityXfRecord) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(id) as times","IFNULL(sum(real_money) ,0) as amount");
        queryWrapper.eq("p_id",entityXfRecord.getPId());
        queryWrapper.eq("com_id",entityXfRecord.getComId());
        queryWrapper.in("cons_state",1,3,5);
        queryWrapper.eq("pay_state",1);
        queryWrapper.eq("if_refund",0);
        queryWrapper.eq("cons_type",entityXfRecord.getConsType());

        if(entityXfRecord.getReserved1()!=null && entityXfRecord.getReserved1().equals("selectMerchTrue")){
            queryWrapper.eq("merch_id",entityXfRecord.getMerchId());
        }

        String strDate = DateUtils.getStrDate(new Date(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        if (entityXfRecord.getConsTime() != null) {
            strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }

        String endMonth = strDate.substring(0,7).replace("-","");
        String startMonth = DateUtils.getLastMonth(endMonth, 1);

        List<String> dates = new ArrayList<>();
        dates.add(endMonth);
        dates.add(startMonth);

        if (entityXfRecord.getMealNo().equals("500")){
            strDate = strDate.substring(0,10);
//            queryWrapper.between( "create_time",strDate+" 00:00:00",strDate+" 23:59:59");
            queryWrapper.eq("meal_date", strDate);
        }else if (entityXfRecord.getMealNo().equals("1000")){
            //按月查询
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
            queryWrapper.between("meal_date",strDate + "-01", endDate);
        }else {
            queryWrapper.eq("meal_no",entityXfRecord.getMealNo());
//            queryWrapper.last("and TO_DAYS(create_time) = TO_DAYS('"+strDate+"')");
            strDate = strDate.substring(0,10);
            queryWrapper.eq("meal_date", strDate);
//            queryWrapper.between( "create_time",strDate+" 00:00:00",strDate+" 23:59:59");
        }
        queryWrapper.in("month_value",dates);
        EntityXfRecord entityXfRecord1 = new EntityXfRecord();
        entityXfRecord1.setTimes(0);
        entityXfRecord1.setAmount(0L);

        super.list(queryWrapper).forEach(entityXfRecordProcess -> {
            entityXfRecord1.setTimes(entityXfRecord1.getTimes() + entityXfRecordProcess.getTimes());
            entityXfRecord1.setAmount(entityXfRecord1.getAmount() + entityXfRecordProcess.getAmount());
        });
//      ======= 需要改动
//        for (String month : dates) {
//            QueryWrapper<EntityXfRecordProcess> queryWrapper2 = queryWrapper.clone();
//            queryWrapper2.eq("month_value", month);
//            EntityXfRecordProcess entityXfRecords3 = super.getOne(queryWrapper2);
//            entityXfRecord1.setTimes(entityXfRecord1.getTimes() + entityXfRecords3.getTimes());
//            if(entityXfRecords3.getAmount()!=null) entityXfRecord1.setAmount(entityXfRecord1.getAmount() + entityXfRecords3.getAmount());
//        }
//      ======= 需要改动
        Map<String,Object> map=new HashMap<>();
        map.put("times",entityXfRecord1.getTimes());
//        if(entityXfRecord1.getAmount()==null){
//            entityXfRecord1.setAmount(0l);
//        }
        map.put("amount",entityXfRecord1.getAmount());
        return ResultUtil.success(map);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Result xfUserparamDiscount(Long companyId,EntityXfRecord entityXfRecord) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(id) as times","IFNULL(sum(discount) ,0) as amount");
        queryWrapper.eq("p_id",entityXfRecord.getPId());
        queryWrapper.eq("com_id",entityXfRecord.getComId());
        queryWrapper.in("cons_state",1,3,5);
        queryWrapper.eq("pay_state",1);
        queryWrapper.eq("if_refund",0);
        queryWrapper.gt("discount",0);
        queryWrapper.eq("cons_type",entityXfRecord.getConsType());

        if(entityXfRecord.getReserved1()!=null && entityXfRecord.getReserved1().equals("selectMerchTrue")){
            queryWrapper.eq("merch_id",entityXfRecord.getMerchId());
        }

//        String strDate=DateUtils.getStrDate(new Date(),DateUtils.YYYY_MM_DD_HH_MM_SS);
//        String start = strDate.substring(0, 7).replace("-", "");
//        List<String> dates = new ArrayList<>();
//        try {
//            dates = DateUtils.getMonthBetween(start, start);
//        } catch (Exception e) {
//            log.info("请求方式:【{}】,查询的请求的数据:【{}】,异常信息:【{}】","获取消费记录信息",entityXfRecord,e);
//        }
//
//        List<String> finalDates = dates;
//        //日期范围查询
//        queryWrapper.and(wrapper -> {
//            for (String info : finalDates) {
//                wrapper.or().eq("month_value", info);
//            }
//        });
        String strDate = DateUtils.getStrDate(new Date(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        if (entityXfRecord.getConsTime() != null) {
            strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }

        String endMonth = strDate.substring(0,7).replace("-","");
        String startMonth = DateUtils.getLastMonth(endMonth, 1);

        List<String> dates = new ArrayList<>();
        dates.add(endMonth);
        dates.add(startMonth);
        Integer mealNo = Integer.valueOf(entityXfRecord.getMealNo());
        if (entityXfRecord.getMealNo().equals("500")){
            //餐段按天
//            queryWrapper.last("and TO_DAYS(create_time) = TO_DAYS('"+strDate+"')");
            strDate = strDate.substring(0,10);
//            queryWrapper.between( "create_time",strDate+" 00:00:00",strDate+" 23:59:59");
            queryWrapper.eq("meal_date", strDate);
        }else if (entityXfRecord.getMealNo().equals("1000")){
            //按月查询
//            queryWrapper.last("and DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT('"+strDate+"','%Y%m')");
//            strDate = strDate.substring(0, 7).replace("-", "");
//            queryWrapper.apply(" DATE_FORMAT(meal_date, '%Y%m') =" + strDate);
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
            queryWrapper.between("meal_date",strDate + "-01", endDate);
        }else if (mealNo>900 && mealNo<1000){
            //按月/某餐查询
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
            queryWrapper.between("meal_date",strDate + "-01", endDate);
            queryWrapper.eq("meal_no" ,mealNo-900 ) ;
        }else {
            queryWrapper.eq("meal_no",entityXfRecord.getMealNo());
//            queryWrapper.last("and TO_DAYS(create_time) = TO_DAYS('"+strDate+"')");
            strDate = strDate.substring(0,10);
            queryWrapper.eq("meal_date", strDate);
//            queryWrapper.between( "create_time",strDate+" 00:00:00",strDate+" 23:59:59");
        }
        queryWrapper.in("month_value",dates);
        EntityXfRecord entityXfRecord1 = new EntityXfRecord();
        entityXfRecord1.setTimes(0);
        entityXfRecord1.setAmount(0L);
        super.list(queryWrapper).forEach(entityXfRecordProcess -> {
            entityXfRecord1.setTimes(entityXfRecord1.getTimes() + entityXfRecordProcess.getTimes());
            entityXfRecord1.setAmount(entityXfRecord1.getAmount() + entityXfRecordProcess.getAmount());
        });
//        =====需要改动
//        for (String month : dates) {
//            QueryWrapper<EntityXfRecordProcess> queryWrapper2 = queryWrapper.clone();
//            queryWrapper2.eq("month_value", month);
//            EntityXfRecordProcess entityXfRecords3 = super.getOne(queryWrapper2);
//            entityXfRecord1.setTimes(entityXfRecord1.getTimes() + entityXfRecords3.getTimes());
//            if(entityXfRecords3.getAmount()!=null) entityXfRecord1.setAmount(entityXfRecord1.getAmount() + entityXfRecords3.getAmount());
//        }

//        需要改动
        Map<String,Object> map=new HashMap<>();
        map.put("times",entityXfRecord1.getTimes());
//        if(entityXfRecord1.getAmount()==null){
//            entityXfRecord1.setAmount(0L);
//        }
        map.put("amount",entityXfRecord1.getAmount());
        return ResultUtil.success(map);
    }



    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateOrderPayState(Long companyId, String platOrderNo) {
        QueryWrapper<EntityXfRecordProcess> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("cons_state", 5);
        queryWrapper.eq("pay_state", 0);
        queryWrapper.eq("plat_order_no", platOrderNo);
        List<EntityXfRecordProcess> processList =  this.list(queryWrapper);

        UpdateWrapper<EntityXfRecordProcess> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("cons_state", 5);
        updateWrapper.eq("pay_state", 0);
        updateWrapper.eq("plat_order_no", platOrderNo);
        updateWrapper.set("pay_state", 1);
        updateWrapper.setSql("pay_time = cons_time");
        boolean bo =  this.update(updateWrapper);
        if(!bo){
            return bo;
        }
        //新增成功表的退款数据
        for (EntityXfRecordProcess entityXfRecordProcess : processList) {
            MqSaveOrderVo mqSaveOrderVo = new MqSaveOrderVo();
            mqSaveOrderVo.setComId(companyId);
            mqSaveOrderVo.setId(entityXfRecordProcess.getId());
            mqSaveOrderVo.setMachNo(entityXfRecordProcess.getDeviceNo());
            saveOrderProductService.saveOrder(JSON.toJSONString(mqSaveOrderVo));
            try {
                log.info("开始打印订单 i68VnR4i 订单：【{}】", entityXfRecordProcess);
                PlanKitchenPrinterRequest planKitchenPrinterRequest = new PlanKitchenPrinterRequest();
                planKitchenPrinterRequest.setComId(entityXfRecordProcess.getComId());
                planKitchenPrinterRequest.setPointId(entityXfRecordProcess.getPointId());
                planKitchenPrinterRequest.setOrderNo(entityXfRecordProcess.getOrderNo());
                planKitchenPrinterRequest.setCheckPrint(true);

                log.info("开始打印订单 8tDnRcC3 planKitchenPrinter：【{}】", planKitchenPrinterRequest);
                Result printResult = payConfigClientService.planKitchenPrinter(planKitchenPrinterRequest);
                log.info("订单打印结果 wGb1zUjJ【{}】", printResult);
            } catch (Exception e) {
                //e.printStackTrace();
                log.info("订单打印异常 MS1ZwZZA 【{}】【{}】", entityXfRecordProcess, e);
            }
        }
        return bo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public EntityXfRecordProcess getReadyOrderVer(Long companyId,Long pointId, Long pId, String mealDate, Integer mealNo,String orderNo) {
        LambdaQueryWrapper<EntityXfRecordProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntityXfRecordProcess::getComId,companyId);
        queryWrapper.eq(EntityXfRecordProcess::getPayState,1);
        queryWrapper.eq(EntityXfRecordProcess::getIfRefund,0);
        queryWrapper.eq(EntityXfRecordProcess::getPointId,pointId);
        queryWrapper.eq(EntityXfRecordProcess::getMealNo,mealNo);
        queryWrapper.eq(EntityXfRecordProcess::getMealDate,mealDate);
        queryWrapper.in(EntityXfRecordProcess::getConsState,3,5);
        if(!CommonUtil.isNull(orderNo)){
            queryWrapper.eq(EntityXfRecordProcess::getOrderNo,orderNo);
            return this.getOne(queryWrapper);
        }
        queryWrapper.eq(EntityXfRecordProcess::getPId,pId);
        queryWrapper.eq(EntityXfRecordProcess::getVerification,0);
        queryWrapper.orderByDesc(EntityXfRecordProcess::getId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<EntityXfRecordProcess> timerTransportOrder(Long companyId,String month,Integer part) {
        return iDaoXfRecordProcess.timerTransportOrder(month,part);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<EntityXfRecordProcess> timerTransportGoodList(Long companyId,String month,Integer part) {
        return iDaoXfRecordProcess.timerTransportGoodList(month,part);
    }
}
